package com.masterclient.app.businessmodule.vipmodule.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.eoner.baselib.utils.status.StatusBarUtils;
import com.eoner.baselib.widget.navgation.FCNavigationBar;
import com.eoner.baselibrary.utils.DensityUtil;
import com.eoner.baselibrary.utils.Utils;
import com.eoner.baselibrary.widget.CommonTabLayout;
import com.eoner.managerlibrary.user.UserDataManager;
import com.eoner.middlelib.router.map.FCRouterPath;
import com.masterclient.app.R;
import com.masterclient.app.base.FCBusinessActivity;
import com.masterclient.app.businessmodule.vipmodule.adapter.FragmentAdapter;
import com.masterclient.app.businessmodule.vipmodule.auth.UploadIDsActivity;
import com.masterclient.app.businessmodule.vipmodule.fragment.ProfitFragment;
import com.masterclient.app.widget.RealNameDialog;
import com.gyf.immersionbar.ImmersionBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 推广收益
 */
public class PromotionProfitActivity extends FCBusinessActivity {

    @BindView(R.id.total_v)
    View totalV;
    @BindView(R.id.total_tv)
    TextView totalTv;
    @BindView(R.id.djs_tv)
    TextView djsTv;
    @BindView(R.id.yjs_tv)
    TextView yjsTv;
    @BindView(R.id.ytx_tv)
    TextView ytxTv;
    @BindView(R.id.tabLayout)
    CommonTabLayout tabLayout;
    @BindView(R.id.appbar)
    AppBarLayout appbar;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.bar_view)
    FrameLayout barView;
    @BindView(R.id.img)
    ImageView img;
    private int scrollH;// 顶部渐变距离
    private List<String> titles;
    @Override
    protected int requireGetLayoutId() {
        return R.layout.activity_promotion_profit;
    }

    @Override
    protected void requireSetCustomNavigationBar(FCNavigationBar navigationBar) {
        navigationBar.setVisibility(View.GONE);
        //设置状态栏
        StatusBarUtils.immersive(this);
        //获取状态栏高度
        int statusBarHeight = ImmersionBar.getStatusBarHeight(getActivity());
        barView.setPadding(0, statusBarHeight, 0, 0);
        // 设置字体样式
        Utils.setDin(totalTv, this);
        Utils.setDin(djsTv, this);
        Utils.setDin(yjsTv, this);
        Utils.setDin(ytxTv, this);
    }

    @Override
    protected void requireInitUIAndData(Bundle savedInstanceState) {
        initFragment();
        addOnOffsetChangedListener();
        getImgH();
    }

    /**
     * 初始化fragment
     */
    private void initFragment(){
        List<String> tabs = new ArrayList<>(3);
        tabs.add("待结算");
        tabs.add("已结算");
        tabs.add("已流失");
        for(int i=0;i<tabs.size();i++){
            tabLayout.addTab(tabs.get(i), i==0);
        }
        List<Fragment> fragments = new ArrayList<>(3);
        // 待结算
        fragments.add(ProfitFragment.newInstance(10));
        // 已结算
        fragments.add(ProfitFragment.newInstance(20));
        // 已流失
        fragments.add(ProfitFragment.newInstance(30));
        FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager(),fragments,tabs);
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.
                TabLayoutOnPageChangeListener(tabLayout.getTabLayout()));
        tabLayout.setupWithViewPager(viewPager);
        adapter.notifyDataSetChanged();
    }

    /**
     * AppBarLayout滑动距离监听
     */
    private void addOnOffsetChangedListener() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            appbar.addOnOffsetChangedListener((appBarLayout, verticalOffset) -> {
                // 根据滑动距离设置顶部透明度
                float offset = Math.abs(verticalOffset);
                float alpha = 255 * (offset / scrollH);
                if (alpha >= 255)
                    alpha = 255;
                setAlpha((int) alpha);
            });
        }
    }

    /**
     * 设置透明度
     *
     * @param alpha
     */
    private void setAlpha(int alpha) {
        barView.getBackground().mutate().setAlpha(alpha);
    }

    /**
     * 获取背景图片的高度
     */
    private void getImgH() {
        img.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int height = img.getHeight();
                if (height > 0) {
                    int statusBarHeight = ImmersionBar.getStatusBarHeight(getActivity());
                    img.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) totalV.getLayoutParams();
                    scrollH = height - statusBarHeight - DensityUtil.dip2px(46);
                    params.height = scrollH;
                    totalV.setLayoutParams(params);
                }
            }
        });
    }

    /**
     * 设置 待结算 已结算等信息
     * @param waitS
     * @param balance
     * @param settleProfit
     * @param totalB
     */
    public void setAmount(String waitS,String balance,String settleProfit,String totalB){
        if(null != totalB)
            totalTv.setText(totalB);
        if(null != waitS)
            djsTv.setText(waitS);
        if(null != settleProfit)
            yjsTv.setText(settleProfit);
        if(null != balance)
            ytxTv.setText(balance);
    }

    /**
     * 设置 Viewpage标题
     * @param titles
     */
    public void setViewpageTitles(List<String> titles){
        if(null == this.titles || this.titles.size()==0){
            this.titles = titles;
            for(int i=0;i<titles.size();i++) {
                tabLayout.refreshTab(i,titles.get(i), i==0);
            }
        }
    }

    @OnClick({R.id.back_iv,R.id.withdrawal_tv})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.back_iv:
                finish();
                break;
            case R.id.withdrawal_tv:// 提现
                isRealName();
                break;
        }

    }

    /**
     * 判断是否实名认证
     */
    private void isRealName(){

        if(UserDataManager.getInstance().getCheckIdCardStatus().equals("1")){
            ARouter.getInstance().build(FCRouterPath.CASH_INDEX).withString("source"
                    ,"1").navigation();
        }else {// 提示去实名认证
            showRealDialog();
        }
    }

    /**
     * 显示实名认证dialog
     */
    private void showRealDialog(){
        new RealNameDialog.Builder(this)
                .setOnConfirmClickListener("去认证", () -> startActivity(new Intent(PromotionProfitActivity.this, UploadIDsActivity.class)))
                .createDialog().show();
    }
}
